import { creepRoleEnum, taskType } from "../../../enum"
import { creepNameToObj, objIDToObj } from "../../../tools/baseTools"
import { changeCreepRule, creepGetSource, creepGetTask, creepIsNotRuning, creepMoveExtesion } from "../../../tools/tools/creepTool"
import taskManager from "../../task/taskManager"

export default {
    run: function (creep) {
        creep = creepNameToObj(creep)
        // console.log('修复无当前任务bug：'+JSON.stringify(creep.memory.currentTaskID));
        let taskPool = [taskType.needResource]
        if (!creep.memory.currentTaskID) {
            let getTaskResult = creepGetTask(creep, taskPool)       // 只获取任务id
            if (!getTaskResult['taskID']) { // 当前任务已经完成，且没有新的需求任务，则切换回freeman规则                
                if (creep.memory.role == creepRoleEnum.worker.freeman) changeCreepRule(creep)
                return
            } else {
                taskManager.getTaskByID(getTaskResult['taskID'], creep.name, creep.pos.roomName)
            }
        }
        if (creep.memory.state.isWorking) {
            creep.say(`搬运工`)
            // 工作逻辑
            ruleCarrierWorking(creep)
        } else {
            creep.say(`搬运工在准备`)
            // 准备逻辑
            creepGetSource({ creep: creep, room: creep.room, sourceType: creep.memory.params.resouce })
        }
    }
}

function ruleCarrierWorking(creep) {
    creep = creepNameToObj(creep)
    let targetObj = objIDToObj(creep.memory.targetID[creep.memory.targetID.length - 1])
    // 计算需要提供的数量
    let amount = Math.min(creep.store.getUsedCapacity(creep.memory.params.resource), creep.memory.params.count)
    if (amount == 0) {  // 当提供的数量为0，说明当前目标不需要了，那么就直接完成任务
        creep.memory.state.isWorking = false
        creepIsNotRuning(creep)
        taskManager.finishTask(creep.memory.currentTaskID, creep.memory.targetPos.roomName, { obj: creep })
        targetObj.checkSelf()
    } else {
        let resultCode = creep.transfer(targetObj, creep.memory.params.resource, amount)
        // console.log(creep.name + `执行transfer的逻辑结果编码${resultCode}`);
        switch (resultCode) {
            case OK:
                creep.memory.params.count -= amount // 获取还需要提供给目标的资源数量
                creepIsNotRuning(creep)
                if (creep.store.getFreeCapacity(creep.memory.params.resource) == 0) {
                    // 背包中没有了可用的资源，那么切换回准备状态
                    creep.memory.state.isWorking = false
                }
                break;
            case ERR_NOT_ENOUGH_RESOURCES:
                creep.memory.state.isWorking = false
                creepIsNotRuning(creep)
                break;
            case ERR_FULL:
                creep.memory.state.isWorking = false
                creepIsNotRuning(creep)
                // 通过currentTaskID来校验任务是否存在，存在则完成任务，不存在就跳过任务
                taskManager.getTaskByID(creep.memory.currentTaskID, creep.name, creep.pos.roomName, false) ? taskManager.finishTask(creep.memory.currentTaskID, creep.memory.targetPos.roomName, { obj: creep }) : ''
                targetObj.checkSelf()
                break;
            case ERR_NOT_IN_RANGE:
                creepMoveExtesion(creep)
                break;
            default:
                creep.memory.state.isRuning = false
                console.log(creep.name + `使用transfer的逻辑可能出现问题了,错误编码${resultCode}`);
                break;
        }
    }
}



// creep = creepNameToObj(creep)
// let targetObj = objIDToObj(creep.memory.targetID[creep.memory.targetID.length - 1])
// // 计算需要提供的数量
// let amount = Math.min(creep.store.getUsedCapacity(creep.memory.params.resource), creep.memory.params.count)
// if (amount == 0) {
//     // 这里有一个bug，但是感觉反而更好用了。。。。。可使用的能量在输送给目标后，amount必定等于0，因此就触发完成任务机制
//     let creepStateMemory = creep.memory.state
//     creepStateMemory.isWorking = false
//     creepIsNotRuning(creep)
//     taskManager.finishTask(creep.memory.currentTaskID, creep.memory.targetPos.roomName, { obj: creep })
//     targetObj.checkSelf()
// }
// let resultCode = creep.transfer(targetObj, creep.memory.params.resource, amount)
// // console.log(`transfer的逻辑可能出现问题了,错误编码${resultCode}`);
// switch (resultCode) {
//     case OK:
//         creep.memory.params.count -= amount
//         creep.memory.state.isRuning = false
//         break;
//     case ERR_NOT_ENOUGH_RESOURCES:
//         creep.memory.state.isWorking = false
//         creep.memory.state.isRuning = false
//         break;
//     case ERR_FULL:
//         let creepStateMemory = creep.memory.state
//         creepStateMemory.isWorking = false
//         creepStateMemory.isRuning = false
//         creepStateMemory.isBlock = false
//         creepStateMemory.path = null
//         creepStateMemory.tick = 0
//         console.log('如果出现完成任务id为空' + creep.name);
//         taskManager.finishTask(creep.memory.currentTaskID, creep.memory.targetPos.roomName, { obj: creep })
//         targetObj.checkSelf()
//         break;
//     case ERR_NOT_IN_RANGE:
//         creepMoveExtesion(creep)
//         break;
//     default:
//         creep.memory.state.isRuning = false
//         console.log(creep.name + `使用transfer的逻辑可能出现问题了,错误编码${resultCode}`);
//         break;
// }